Conditions | 7 |
Total Lines | 94 |
Code Lines | 80 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | import { |
||
43 | |||
44 | @Get() |
||
45 | @WithName('faircalendar_index') |
||
46 | @Render('pages/faircalendar/index.njk') |
||
47 | public async get( |
||
48 | @Query() dto: FairCalendarControllerDTO, |
||
49 | @LoggedUser() user: User, |
||
50 | @Req() req: Request |
||
51 | ) { |
||
52 | let date = new Date(); |
||
53 | |||
54 | if (dto.year !== undefined && dto.month !== undefined) { |
||
55 | date = new Date(dto.year, dto.month - 1, 15); |
||
56 | } |
||
57 | |||
58 | const userId = dto.userId ? dto.userId : user['id']; |
||
59 | |||
60 | const users: UserView[] = await this.queryBus.execute( |
||
61 | new GetUsersQuery(false, true) |
||
62 | ); |
||
63 | |||
64 | const events: FairCalendarView[] = await this.queryBus.execute( |
||
65 | new GetMonthlyFairCalendarQuery(date, userId) |
||
66 | ); |
||
67 | |||
68 | const overview = await this.overviewFactory.create(events); |
||
69 | const overviewTable = this.overviewTableFactory.create(overview); |
||
70 | |||
71 | const fullCalendarEvents = events.map(event => { |
||
72 | let title = `${minutesToHours(event.time)} - `; |
||
73 | |||
74 | const fcEventType = event.type.startsWith('leave_') |
||
75 | ? 'leave' |
||
76 | : event.type; |
||
77 | |||
78 | if (fcEventType === 'mission' && event.task && event.project) { |
||
79 | title += `${event.project.name} (${event.task.name})`; |
||
80 | } else if (fcEventType === 'leave') { |
||
81 | title += `${this.translator.translate('leaves-type-value', { |
||
82 | type: event.type.slice(6) |
||
83 | })}`; |
||
84 | } else { |
||
85 | title += `${this.translator.translate('faircalendar-type-option', { |
||
86 | type: event.type |
||
87 | })}`; |
||
88 | } |
||
89 | |||
90 | const extendedProps: Record<string, any> = { |
||
91 | summary: event.summary |
||
92 | }; |
||
93 | |||
94 | if (event.id) { |
||
95 | extendedProps.url = this.resolver.resolve('faircalendar_events_edit', { |
||
96 | id: event.id |
||
97 | }); |
||
98 | } |
||
99 | |||
100 | return { |
||
101 | // See: https://fullcalendar.io/docs/event-object |
||
102 | id: event.id, |
||
103 | type: fcEventType, |
||
104 | start: event.date, |
||
105 | end: event.date, |
||
106 | title, |
||
107 | extendedProps, |
||
108 | textColor: `var(--event-${fcEventType}-text)`, |
||
109 | backgroundColor: `var(--event-${fcEventType}-background)`, |
||
110 | borderColor: `var(--event-${fcEventType}-border)` |
||
111 | }; |
||
112 | }); |
||
113 | |||
114 | const eventsByStartDate = ArrayUtils.groupBy( |
||
115 | fullCalendarEvents, |
||
116 | event => event.start |
||
117 | ); |
||
118 | |||
119 | const listViewDays = this.dateUtils.getWeekDaysOfMonth(date).map(day => { |
||
120 | return [ |
||
121 | day, |
||
122 | eventsByStartDate[this.dateUtils.format(day, 'yyyy-MM-dd')] || [] |
||
123 | ]; |
||
124 | }); |
||
125 | |||
126 | return { |
||
127 | users, |
||
128 | overviewTable, |
||
129 | fullCalendarEvents, |
||
130 | date, |
||
131 | currentMonth: date.getMonth() + 1, |
||
132 | currentYear: date.getFullYear(), |
||
133 | userId, |
||
134 | viewName: req.cookies.faircalendar_view, |
||
135 | listViewDays, |
||
136 | isCalendarOfLoggedUser: userId === user.getId() |
||
137 | }; |
||
140 |